package com.woniuxy.springcloud.provider.student.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.springcloud.common.entity.PageDto;
import com.woniuxy.springcloud.common.entity.ResponseResult;
import com.woniuxy.springcloud.common.entity.Student;
import com.woniuxy.springcloud.provider.student.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * description: StudentController <br>
 * date: 2022/1/19 6:37 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

@RefreshScope
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    StudentService studentService;

    @Value("${server.port}")
    int port;

    @PostMapping("/")
    public ResponseResult addStudent(@RequestBody Student student) {
        log.info("student:" + student);
        studentService.addStudent(student);
        return new ResponseResult(200, "ok", null);
    }

    @GetMapping("/{sid}")
    public ResponseResult<Student> getStudent(@PathVariable("sid") Integer sid) {
        return new ResponseResult(200, "ok--port:" + port, studentService.getStudentBySid(sid));
    }

    @GetMapping("/")
    public ResponseResult<List<Student>> getAllStudent() {
        return new ResponseResult(200, "ok---port:" + port, studentService.getAllStudents());
    }

    @PutMapping("/")
    public ResponseResult<Student> updateStudentBySid(@RequestBody Student student) {
        Integer i = studentService.updateStudentBySid(student);
        if (i > 0) {
            return new ResponseResult(200, "修改", "成功新增" + port + i + "条数");
        } else {
            return new ResponseResult(500, "修改失败", "服务器在维护中");
        }
    }

    @DeleteMapping("/{sid}")
    public ResponseResult<Integer> deleteOneStudent(@PathVariable("sid") Integer sid) {
        Integer i = studentService.deleteOne(sid);
        if (i > 0) {
            return new ResponseResult(200, "删除", "成功新增" + port + i + "条数");
        } else {
            return new ResponseResult(500, "删除失败", "服务器在维护中");
        }
    }
    //分页带条件

    @RequestMapping("/findAllStudent")
    public ResponseResult<PageInfo<Student>> findAllStudent(@RequestBody PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getLimit());
        List<Student> studentList = studentService.findAllAndPageination(pageDto);
        PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
        return new ResponseResult<>(200, "查询成功！", studentPageInfo);
    }

    //超时控制

    @GetMapping("/timeout")
    public ResponseResult<String> timeout() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ResponseResult(200, "ok" + port, "timeout");
    }

    @GetMapping("/ok")
    public ResponseResult<String> ok() {
        return new ResponseResult(200, "ok", "ok");
    }

    @Value("${testconfig}")
    String testconfig;

    @GetMapping("/testconfig")
    public ResponseResult<String> testconfig() {
        return new ResponseResult<String>(200, "testconfig", testconfig);
    }
}
